/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Sep 4, 2008

package com.sceneworker.util.swing.timelineeditor;

import java.awt.Dimension;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

public class KeyFrameTimeLinePanel extends JPanel implements AdjustmentListener{
	
	protected	TimeDivisionPanel		mcl_divisionPanel;
	protected	TimeSliderPanel			mcl_sliderPanel;
	protected	KeyFramesPanel			mcl_keyFrames;
	protected	JScrollBar				mcl_scrollBar;
	protected	int						mi_maxTime;
	
	static		final	public		Dimension	kMaxDim = new Dimension(760, 15);
	
	public	KeyFrameTimeLinePanel(int i_maxTime) {
		super();
		
		mi_maxTime = i_maxTime;
		
		buildPanel();
	}
	
	public	KeyFramesPanel		getKeyFramesPanel() {
		return mcl_keyFrames;
	}
	
	public	TimeSliderPanel		getTimeSliderPanel() {
		return mcl_sliderPanel;
	}
	
	private		void	buildPanel() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		buildTimeLinePanel();
	}
	
	private		void	buildTimeLinePanel() {
		mcl_divisionPanel = new TimeDivisionPanel(this);
		mcl_sliderPanel = new TimeSliderPanel(this);
		mcl_sliderPanel.setStartAndEnd(0, mi_maxTime);
		
		mcl_keyFrames = new KeyFramesPanel(this);
		
		mcl_scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
		mcl_scrollBar.setMaximum(mi_maxTime);
		mcl_scrollBar.addAdjustmentListener(this);
		
		JPanel		lcl_panel = new JPanel();
		lcl_panel.setLayout(new BoxLayout(lcl_panel, BoxLayout.Y_AXIS));
		
		lcl_panel.add(mcl_divisionPanel);
		lcl_panel.add(mcl_sliderPanel);
		lcl_panel.add(mcl_keyFrames);
		lcl_panel.add(mcl_scrollBar);
		add(lcl_panel);
	}

	public	int	getStartTimeOfBar() {
		return mcl_scrollBar.getValue();
	}
	
	public	int	getEndTimeOfBar() {
		return getStartTimeOfBar() + mcl_scrollBar.getModel().getExtent();
	}
	
	public	void	pushBar(boolean b_forwards) {
		if(b_forwards) {
			mcl_scrollBar.setValue(mcl_scrollBar.getValue() + 1);
		} else {
			mcl_scrollBar.setValue(mcl_scrollBar.getValue() - 1);
		}
	}
	
	public	int	getPixelsPerSecond() {
		int		li_start = getStartTimeOfBar();
		int		li_end = getEndTimeOfBar();
		return getWidth() / (li_end - li_start);
	}
	
	public	int	getTotalTime() { 
		return mcl_scrollBar.getMaximum(); 
	}

	public void adjustmentValueChanged(AdjustmentEvent cl_e) {
		if(cl_e.getSource() == mcl_scrollBar) {
			repaint();
		}
	}
		
}
